package com.wing.wcabinet.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.wcabinet.model.entity.WauthorityManage;
import com.wing.wcabinet.model.request.WauthorityManageForm;
import com.wing.wcabinet.model.response.WauthorityManageVO;
import com.wing.wcabinet.service.WauthorityManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-权限管理表管理", tags = "#/wcabinet/wauthorityManage")
@RestController("wauthorityManageController")
@RequestMapping("wcabinet/admin/wauthorityManage")
@RequiredArgsConstructor
public class WauthorityManageController {

    private final WauthorityManageService authorityManageService;


    @ApiOperation(value = "查询所有权限管理", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<WauthorityManageVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,@RequestParam(defaultValue = "cabinetId") String cabinetId
            ,String name
    ){

        Page<WauthorityManage> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if(StringUtils.isNotBlank(cabinetId)){
            params.put("cabinetId", cabinetId);
        }
        if(StringUtils.isNotBlank(name)){
            params.put("name", name);
        }

        IPage<WauthorityManageVO> list = authorityManageService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加权限管理", notes = "添加")
    //@RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
   // public JsonResult add(@Validated(AuthorityManageForm.Add.class) AuthorityManageForm authorityManageForm) {
        public JsonResult add(@RequestBody WauthorityManageForm authorityManageForm) {
        if (authorityManageService.add(authorityManageForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改权限管理", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@RequestBody WauthorityManageForm authorityManageForm) {
        if (authorityManageService.updateAuthorityManage(authorityManageForm) > 0) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除权限管理", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorityManageId", value = "权限管理表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long authorityManageId) {
        if (authorityManageService.removeAll(authorityManageId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "是否启用权限组", notes = "是否启用权限组")
    @PutMapping("/enable/{id}/{type}")
    public JsonResult enable(@PathVariable("id") Long id,@PathVariable("type") String type) {
        if (authorityManageService.enable(id,type)>0) {
            if(type.equals("0")){
                return JsonResult.success("启用成功");
            }else if(type.equals("1")){
                return JsonResult.success("禁用成功");
            }

        }
        return JsonResult.fail("修改失败");
    }


    @ApiOperation(value = "查询可添加的物料", notes = "查询")
    @GetMapping("/findMateriel")
    public PageResult findMateriel(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,String ids
            ,Long authorityManageId
            ,String categoryIds
            ,String skuName
            ,String skuCode
            ,String oldSpecCode
            ,String productBrandName
    ){
        return authorityManageService.findMateriel(page,limit,ids,authorityManageId,categoryIds,skuName,skuCode,oldSpecCode,productBrandName);
    }
}
